package ml.tewas.weaponassignment;

import java.util.List;
import java.util.Map;

import ml.tewas.commondata.ScenarioFinalStateEnum;
import ml.tewas.commondata.ThreatEvaluationResultStruct;

/**
 * The Class WeaponAssignmentDecisionMaker.
 */
public class WeaponAssignmentDecisionMaker implements
		IWeaponAssignmentDecisionMaker {

	/** The is training. */
	private boolean isTraining;

	/** The state list. */
	private List<TewasSimulationState> stateList;

	/** The data service. */
	private IWeaponAssignmentDataService dataService = new WeaponAssignmentDataService();

	/** The learner. */
	private IWeaponAssignmentQLearner learner = new WeaponAssignmentQLearner();

	/**
	 * Instantiates a new weapon assignment decision maker.
	 */
	public WeaponAssignmentDecisionMaker() {

		stateList = dataService.readQTableData();
		learner.clearActionList();
	}

	/* (non-Javadoc)
	 * @see ml.tewas.weaponassignment.IWeaponAssignmentDecisionMaker#scenarioStarted()
	 */
	public void scenarioStarted() {
	

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ml.tewas.weaponassignment.IWeaponAssignmentDecisionMaker#getWeaponAssignedThreatID(java.util.Map)
	 */
	public int getWeaponAssignedThreatID(
			Map<Integer, ThreatEvaluationResultStruct> state) {

		TewasSimulationState similarState = findSimilarState(state);

		learner.addWeaponAssignmentToActionList(similarState,
				WeaponAssignmentUtil.findMostCriticalThreat(similarState));

		return WeaponAssignmentUtil.findMostCriticalThreat(similarState, state);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ml.tewas.weaponassignment.IWeaponAssignmentDecisionMaker#setWeaponAssignedThreatIDForOtherAgent(java.util.Map,
	 *      int)
	 */
	public void setWeaponAssignedThreatIDForOtherAgent(
			Map<Integer, ThreatEvaluationResultStruct> evaluationResult,
			int threatID) {

		TewasSimulationState similarState = findSimilarState(evaluationResult);
		learner.addOtherAgentWeaponAssignment(similarState, evaluationResult
				.get(threatID));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ml.tewas.weaponassignment.IWeaponAssignmentDecisionMaker#scenarioFinished(ml.tewas.commondata.ScenarioFinalStateEnum)
	 */
	public void scenarioFinished(ScenarioFinalStateEnum finalState) {

		if (isTraining) {
			learner.updateStateList(stateList, finalState);
			dataService.writeQTableData(stateList);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ml.tewas.weaponassignment.IWeaponAssignmentDecisionMaker#isTraining()
	 */
	public boolean isTraining() {

		return isTraining;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ml.tewas.weaponassignment.IWeaponAssignmentDecisionMaker#setTraining(boolean)
	 */
	public void setTraining(boolean isTraining) {

		this.isTraining = isTraining;
	}

	/**
	 * Find similar state.
	 * 
	 * @param state
	 *            the state
	 * 
	 * @return the tewas simulation state
	 */
	private TewasSimulationState findSimilarState(
			Map<Integer, ThreatEvaluationResultStruct> state) {

		TewasSimulationState currentState = new TewasSimulationState(state);

		for (TewasSimulationState item : stateList) {
			if (item.equals(currentState)) {
				return item;
			}
		}
		return currentState;
	}
}
